package com.hello.sky.controller.admin;

import com.hello.sky.model.dto.DishDTO;
import com.hello.sky.model.dto.DishPageQueryDTO;
import com.hello.sky.model.entity.Dish;
import com.hello.sky.model.vo.DishVO;
import com.hello.sky.result.PageResult;
import com.hello.sky.result.Result;
import com.hello.sky.service.CategoryService;
import com.hello.sky.service.DishService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品 前端控制器
 * </p>
 *
 * @author D
 */
@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService  dishService;
    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "新增菜品")
    @PostMapping()
    public Result addDish(@RequestBody DishDTO dishDTO){
        dishService.addDish(dishDTO);
        return Result.success();
    }

    @Operation(summary = "菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);//后绪步骤定义
        return Result.success(pageResult);
    }

    @DeleteMapping
    @Operation(summary = "菜品批量删除")
    public Result delete(@RequestParam("ids") List<Long> ids) {
        log.info("菜品批量删除：{}", ids);
        dishService.deleteDishes(ids);//后绪步骤实现
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询菜品")
    public Result<DishVO> getById(@PathVariable("id") Long id) {
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @Operation(summary = "修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.updateDishWithFlavor(dishDTO);
        return Result.success();
    }

    @Operation(summary = "根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> getDishById(@RequestParam("categoryId") Long categoryId){
        List<DishVO> dishList = categoryService.getDishById(categoryId);
        return Result.success(dishList);
    }

    @Operation(summary = "改变菜品状态")
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable("status") Integer status, @RequestParam("id") Long id){
        dishService.changeStatus(status, id);
        return Result.success();
    }


}
